package com.yixun.qfbao.dao.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.dao.DeptPermissionDao;
import com.yixun.qfbao.mapper.DeptPermissionMapper;
import com.yixun.qfbao.model.DeptPermission;
import com.yixun.qfbao.model.Node;
import com.yixun.qfbao.model.dto.DeptPermissionDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service("deptPermissionDao")
public class DeptPermissionDaoImpl implements DeptPermissionDao {
	
	@Autowired
	private DeptPermissionMapper mapper;

	@Transactional(readOnly = true)
	public Page<DeptPermission> getPageModel(DeptPermissionDto entity, int begin, int end){
		int total = mapper.count(entity);
		List<DeptPermission> rows = mapper.list(entity,begin,end);
		return new Page<>(total, rows);
	}

	public List<DeptPermission> selectListByDto(DeptPermissionDto entity){
		return mapper.selectListByDto(entity);
	}

	@Override
	public List<DeptPermission> selectListByExample(Example example) {
		return mapper.selectByExample(example);
	}

	public DeptPermission selectByDto(DeptPermissionDto entity){
		return mapper.selectByDto(entity);
	}
	
	public DeptPermission selectById(Long id){
		if(Objects.nonNull(id)){
			return mapper.selectByPrimaryKey(id);
		}
		return null;
	}

	@Override
	public int count(DeptPermissionDto entity) {
		return mapper.count(entity);
	}

	public DeptPermission create(DeptPermission entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		mapper.insertSelective(entity);
		return entity;
	}

	public int createList(List<DeptPermission> entityList) {
		return mapper.insertList(entityList);
	}

	public int update(DeptPermission entity) {
		// 修改时必须判断 id 是否为null,否则很危险
		// 如果为null,会修改全部.最终where语句为 (id is not null),没有 id = ???
		if(Objects.isNull(entity.getId())){
			return 0;
		}
		entity.setUpdateTime(Calendar.getInstance().getTime());
		Example example = new Example(DeptPermission.class);
		example.createCriteria()
				.andIsNotNull("id")
				.andEqualTo("id", entity.getId());
		return mapper.updateByExampleSelective(entity, example);
	}

	public int updateByIds(DeptPermission entity, List<Long> updateIds) {
		// 修改时必须判断 id 是否为null,否则很危险
		// 如果为null,会修改全部.最终where语句为 (id is not null),没有 id = ???
		if(CollectionUtils.isEmpty(updateIds)){
			return 0;
		}
		entity.setUpdateTime(Calendar.getInstance().getTime());
		Example example = new Example(DeptPermission.class);
		example.createCriteria()
				.andIsNotNull("id")
				.andIn("id", updateIds);
		return mapper.updateByExampleSelective(entity, example);
	}

	public int saveOrUpdate(DeptPermission entity) {
		if(Objects.nonNull(entity) && Objects.nonNull(entity.getId())){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}

	@Override
	public List<Node> getBindNodes(DeptPermissionDto entity) {
		return mapper.getBindNodes(entity);
	}
}
